package com.kakaxi.mybilibili.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.entity.HorizontalVideoMultiItem;
import com.kakaxi.mybilibili.bean.HorizontalVideoItemBean;

import java.util.List;

/**
 * 横向视频列表适配器
 * Created by kakaxi on 16/11/24.
 */

public class HorizontalVideoRecyAdapter extends
        BaseMultiItemQuickAdapter<HorizontalVideoMultiItem, BaseViewHolder> {


    public HorizontalVideoRecyAdapter(List<HorizontalVideoMultiItem> data) {
        super(data);
        addItemType(HorizontalVideoMultiItem.TYPE_HEADER, R.layout
                .item_recy_common_horizontal_video_header);
        addItemType(HorizontalVideoMultiItem.TYPE_CONTENT, R.layout
                .item_recy_common_horizontal_video_content);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, HorizontalVideoMultiItem
            horizontalVideoMultiItem) {
        switch (horizontalVideoMultiItem.getItemType()) {
            case HorizontalVideoMultiItem.TYPE_HEADER:
                onBindHeaderViewHolder(baseViewHolder, horizontalVideoMultiItem.getData());
                break;
            case HorizontalVideoMultiItem.TYPE_CONTENT:
                onBindContentViewHolder(baseViewHolder, horizontalVideoMultiItem.getData());
                break;
        }

    }

    /**
     * 绑定标题持久化
     *
     * @param baseViewHolder 视图
     * @param object         数据
     */
    private void onBindHeaderViewHolder(BaseViewHolder baseViewHolder, Object object) {
        String title = (String) object;
        baseViewHolder.setText(R.id.txt_common_horizontal_video_header_title, title);
    }

    /**
     * 绑定内容持久化
     *
     * @param baseViewHolder 视图
     * @param object         数据
     */
    private void onBindContentViewHolder(BaseViewHolder baseViewHolder, Object object) {
        HorizontalVideoItemBean horizontalVideoItemBean = (HorizontalVideoItemBean) object;
        baseViewHolder
                .setText(R.id.txt_common_horizontal_video_title, horizontalVideoItemBean.getTitle())
                .setText(R.id.txt_common_horizontal_video_name, horizontalVideoItemBean.getAuthor())
                .setText(R.id.txt_common_horizontal_video_play, horizontalVideoItemBean.getPlay())
                .setText(R.id.txt_common_horizontal_video_danmaku, horizontalVideoItemBean
                        .getVideoReview());
        Glide.with(mContext).load(horizontalVideoItemBean.getPic()).crossFade().into
                ((ImageView) baseViewHolder.getView(R.id.img_common_horizontal_video_face));
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (recyclerView.getLayoutManager() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        }
    }
}
